/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.variable.expressions;

import java.io.Serializable;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Main
 */
public abstract class CustomExpression implements Serializable {
    Variable evaluatedExpression;
    Object expression;

    /** Creates a new instance of CustomExpression */
    public CustomExpression(String value, DataType returnDataType) {
        evaluatedExpression = new Variable("eval", returnDataType, null, true);
        expression = parseExpression(value);
    }
    
    protected abstract Object parseExpression(String value);

    public abstract Object evaluate(ChainNode context);
    
    public Variable evaluateAtHome(ChainNode context, String name) {
        evaluatedExpression.setValue(evaluate(context), true);
        return new Variable(name, evaluatedExpression.getDataType(), evaluatedExpression.getValue(context), true);
    }
    
    public DataType getExprReturnType() { return evaluatedExpression.getDataType(); }

    @Override
    public String toString() {
        return "<function>";
    }
}
